package com.example.tabbar.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.databinding.ItemRolesBinding
import com.junray.usermangement.room.entity.Role

class RoleAdapter: RecyclerView.Adapter<RoleAdapter.ViewHolder>() {
    private var items: List<Role>? = null
    private var listener: ((Role,String) -> Unit)? = null
    fun setOnItemClickListener(listener: (Role,String) -> Unit) {
        this.listener = listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemRolesBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items?.get(position)
        if (item != null) {
            holder.number.text = (position + 1).toString()
            holder.role.text = item.name
        }

        holder.icon.setOnClickListener {
            listener?.invoke(item!!, item.name!!)
        }
    }

    override fun getItemCount(): Int {
        return items?.size ?: 0
    }

    fun setItems(items: List<Role>?) {
        this.items = items
        notifyDataSetChanged()
    }

    class ViewHolder(binding: ItemRolesBinding)
        : RecyclerView.ViewHolder(binding.root) {
        var number:TextView = binding.number
        var name:TextView = binding.username
        var role:TextView = binding.roleName
        var icon = binding.details
    }

}